package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class AccountManagementActivity extends AppCompatActivity {

    private static final int ADD_ACCOUNT_REQUEST = 1001;
    private RecyclerView accountRecyclerView;
    private AccountAdapter accountAdapter;
    private List<Account> accountList = new ArrayList<>();
    private Switch adminModeSwitch;
    private View adminFunctionsLayout;
    private Button addArticleBtn, addProductBtn, addAccountBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_management);

        // 初始化视图
        initViews();

        // 初始化示例数据
        initSampleData();

        // 设置RecyclerView
        setupRecyclerView();

        // 设置监听器
        setupListeners();

        // 初始化管理员模式状态
        initAdminModeState();
    }

    private void initViews() {
        accountRecyclerView = findViewById(R.id.accountRecyclerView);
        adminModeSwitch = findViewById(R.id.adminModeSwitch);
        adminFunctionsLayout = findViewById(R.id.adminFunctionsLayout);
        addArticleBtn = findViewById(R.id.addArticleBtn);
        addProductBtn = findViewById(R.id.addProductBtn);
        addAccountBtn = findViewById(R.id.addAccountBtn);
    }

    private void initSampleData() {
        accountList.add(new Account("1", "admin@test.com", "管理员", "admin"));
        accountList.add(new Account("2", "user1@test.com", "普通用户", "user"));
        accountList.add(new Account("3", "user2@test.com", "普通用户", "user"));
    }

    private void setupRecyclerView() {
        accountAdapter = new AccountAdapter(accountList);
        accountRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        accountRecyclerView.setAdapter(accountAdapter);

        // 短按跳转修改密码
        accountAdapter.setOnItemClickListener(account -> {
            Intent intent = new Intent(this, ChangePasswordActivity.class);
            intent.putExtra("account_id", account.getId());
            startActivity(intent);
        });

        // 长按删除账号
        accountAdapter.setOnItemLongClickListener(this::showDeleteDialog);
    }

    private void setupListeners() {
        // 添加账号按钮
        addAccountBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            intent.putExtra("is_adding_account", true);
            startActivityForResult(intent, ADD_ACCOUNT_REQUEST);
        });

        // 管理员模式开关
        adminModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            toggleAdminFunctions(isChecked);
            saveAdminModeState(isChecked);
        });

        // 管理员功能按钮
        addArticleBtn.setOnClickListener(v -> {
            startActivity(new Intent(this, AddArticleActivity.class));
        });

        addProductBtn.setOnClickListener(v -> {
            startActivity(new Intent(this, AddProductActivity.class));
        });
    }

    private void initAdminModeState() {
        SharedPreferences prefs = getSharedPreferences("AppPrefs", MODE_PRIVATE);
        boolean savedAdminMode = prefs.getBoolean("admin_mode", false);
        adminModeSwitch.setChecked(savedAdminMode);
        toggleAdminFunctions(savedAdminMode);
    }

    private void showDeleteDialog(Account account) {
        new AlertDialog.Builder(this)
                .setTitle("删除账号")
                .setMessage("确定要删除 " + account.getUsername() + " 吗？")
                .setPositiveButton("删除", (dialog, which) -> deleteAccount(account))
                .setNegativeButton("取消", null)
                .show();
    }

    private void deleteAccount(Account account) {
        accountList.remove(account);
        accountAdapter.notifyDataSetChanged();
        Toast.makeText(this, "已删除账号", Toast.LENGTH_SHORT).show();
    }

    private void toggleAdminFunctions(boolean show) {
        adminFunctionsLayout.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    private void saveAdminModeState(boolean isEnabled) {
        SharedPreferences.Editor editor = getSharedPreferences("AppPrefs", MODE_PRIVATE).edit();
        editor.putBoolean("admin_mode", isEnabled);
        editor.apply();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADD_ACCOUNT_REQUEST && resultCode == RESULT_OK) {
            // 刷新账号列表
            accountList.clear();
            initSampleData();
            accountAdapter.notifyDataSetChanged();
        }
    }
}